<?php
/**
 * Created by PhpStorm.
 * User: juwangkeji
 * Date: 2020-03-24
 * Time: 09:35
 */

namespace controllers\admin;

use addons\widget\controllers\system;
use controllers\AdminApi;
use models\SysModule;
use models\SysUsers;
use system\Factory;
use system\Tool\Upload;

/**
 * Class index
 * @package controllers\admin
 * @auth    全局配置
 */
class index extends AdminApi {

    /**
     * @return array
     * @white
     */
    public function index() {
        if ($this->isValid()) {
        	$addons = [];
        	if (addonIs('widget')){
        		$system = new system();
				$addons['widget'] = $system->index();
			}
			$data = [
				'addons' => $addons ,
			];
            return self::returnJson($data, 0);
        }
    }

    /**
     * 上传图片
     * @return string
     * @white
     */
    public function upload() {
        $file   = values('files.file');
        $upload = new Upload($file, true);

        $status = $upload->upload();
        $path   = $url = '';
        if ($status['info']['path']) {
            $url  = $status['info']['path'];
            $path = self::base64EncodeImage('.' . $url);
        }

        return success(['path' => $path, 'url' => $url]);
    }

    /**
     * 获取用户信息
     * @return array
     * @white
     */
    public function getUserInfo() {
        if ($this->requestValid) {
            $map   = [
                'id' => $this->uid,
            ];
            $model = new SysUsers();
            $find  = $model->where($map)->field('nickname,account')->find();

            return self::returnJson($find, 0);
        }
    }

    /**
     * 保存用户信息
     * @return array|string
     * @white
     */
    public function saveUserInfo() {
        if ($this->requestValid) {
            $data  = self::$post;
            $map   = [
                'id' => $this->uid,
            ];
            $model = new SysUsers();
            $find  = $model->where($map)->find();
            if (!$find) {
                return error('无效的用户');
            }
            $model->where($map)->update(['nickname' => $data['nickname']]);

            return self::returnJson('修改成功', 0);
        }
    }

    /**
     * 修改密码
     * @return array|string
     * @white
     */
    public function savePassword() {
        if ($this->requestValid) {
            $data = self::$post;
            if (!$data['oldPassword'] || !$data['repassword']) {
                return error('请输入密码');
            }
            $map   = [
                'id' => $this->uid,
            ];
            $model = new SysUsers();
            $find  = $model->where($map)->find();
            if (!$find) {
                return error('无效的用户');
            }
            if (!$model->verifyPassword($data['oldPassword'], $find['password'])) {
                return error('原密码不正确');
            }
            $password = $model->generatePassword($data['repassword']);
            $model->where($map)->update(['password' => $password]);
            parent::logout();

            return self::returnJson('修改成功');
        }
    }

    /**
     * 注销登录
     * @return array
     * @white
     */
    public function logout() {
        parent::logout();

        return self::returnJson('success');
    }

    /**
     * 检查状态
     * @return array
     * @white
     */
    public function status() {
        $model    = new SysUsers();

        $nickname = S('nickname' . $this->uid);
        if (!$nickname) {
            $nickname = $model->getFind($this->uid, 'nickname');
            S('nickname' . $this->uid, $nickname, 0);
        }

        return self::returnJson($nickname);
    }

    /**
     * 验证码
     * @return string
     */
    public function verifyCode() {
        $code = Factory::CreateCode();

        return $code->doimg();
    }

    /**
     * 获取导航
     * @return array
     */
    public function getNavs() {
        $data = [
            ['title' => '主页', 'icon' => 'layui-icon-home', 'jump' => '/'],
            [
                'name'  => 'module',
                'title' => '模块管理',
                'icon'  => 'layui-icon-set',
				'auth_name' => 'Modules'
            ],
            [
                'name'  => 'admin',
                'title' => '系统管理',
                'icon'  => 'layui-icon-set',
                'list'  => [
                    ['name' => 'users', 'title' => '账户管理', 'auth_name' => 'SysUsers', 'action' => 'index'],
                    ['name' => 'group', 'title' => '权限组管理', 'auth_name' => 'AuthGroup', 'action' => 'index'],
                    ['name' => 'logs', 'title' => '操作日志', 'auth_name' => 'Index', 'action' => 'logs'],
                    ['name' => 'config', 'title' => '基础设置', 'auth_name' => 'Index', 'action' => 'config'],
                ]
            ],
        ];
        // 读取已安装的模块内置的导航
        $sysModule = new SysModule();
        $list = $sysModule->field('name')->order('id asc')->select();
        $modulesApi = new Modules();
        foreach ($list as $key => $val){
            if($class = $modulesApi->getClass($val['name'])){
                $navs = $class->onInitMenu($data);
                if (count($navs) > 0){
                    $data = array_merge($data , $navs);
                }
            }
        }
        $navs = self::getAuthNavs($data);

        return [
            'code' => 0,
            'msg'  => '',
            'data' => $navs,
        ];
    }
}
